package com.minjiang.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.minjiang.common.result.Result;
import com.minjiang.model.system.*;
import com.minjiang.model.vo.FoodDetailQueryVo;
import com.minjiang.model.vo.FoodDetailVo;
import com.minjiang.model.vo.FoodTypeVo;
import com.minjiang.model.vo.MyDynamicVo;
import com.minjiang.system.service.sec.FoodDetailService;
import com.minjiang.system.service.sec.FoodDetailTypeService;
import com.minjiang.system.service.sec.FoodTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "食物详情管理")
@RestController
@RequestMapping("/admin/system/foodDetail")
public class FoodDetailController {
    @Autowired
    private FoodDetailService foodDetailService;
    @Autowired
    private FoodTypeService foodTypeService;
    @Autowired
    private FoodDetailTypeService foodDetailTypeService;
    @ApiOperation(value = "获取食物详情分页列表")
    @GetMapping("/{page}/{limit}")
    public Result index(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "userQueryVo", value = "查询对象", required = false)
                    FoodDetailQueryVo foodDetailQueryVo) {
        Page<FoodDetail> pageParam = new Page<>(page, limit);
        IPage<FoodDetail> pageModel = foodDetailService.selectPage(pageParam, foodDetailQueryVo);

        List<FoodDetail> records = pageModel.getRecords();
        List<FoodDetailVo> foodDetailVos=new ArrayList<>();
        for (FoodDetail record : records) {
            FoodDetailVo foodDetailVo = new FoodDetailVo();
            BeanUtils.copyProperties(record,foodDetailVo);
            FoodDetailType foodDetailType = foodDetailTypeService.getById(record.getTypeId());

            foodDetailVo.setTypeName(foodDetailType.getTypeName());
            foodDetailVos.add(foodDetailVo);
        }
        Page<FoodDetailVo> foodDetailVoPage = new Page<FoodDetailVo>();
        foodDetailVoPage.setTotal(pageModel.getTotal());
        foodDetailVoPage.setCurrent(pageModel.getCurrent());
        foodDetailVoPage.setRecords(foodDetailVos);
        foodDetailVoPage.setSize(pageModel.getSize());
        foodDetailVoPage.setPages(pageModel.getPages());

        return Result.ok(foodDetailVoPage);
    }

    @ApiOperation(value = "根据id获取食物详情")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable String id) {
        FoodDetail foodDetail = foodDetailService.getById(id);
        FoodDetailType foodDetailType = foodDetailTypeService.getById(foodDetail.getTypeId());
        FoodDetailVo foodDetailVo = new FoodDetailVo();
        BeanUtils.copyProperties(foodDetail,foodDetailVo);
        foodDetailVo.setTypeName(foodDetailType.getTypeName());
        return Result.ok(foodDetailVo);
    }


    @ApiOperation(value = "删除食物详情")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable String id){
        boolean isOk = foodDetailService.removeById(id);
        if (isOk){
            return Result.ok();
        }
        return Result.fail("删除食物详情失败");
    }

    @ApiOperation(value = "添加食物详情")
    @PostMapping("/save")
    public Result save(@RequestBody FoodDetail foodDetail){
        boolean save = foodDetailService.save(foodDetail);
        if (save){
            return Result.ok();
        }
        return Result.fail("添加食物详情失败");
    }

    @ApiOperation(value = "更新食物详情")
    @PutMapping("/update")
    public Result updateById(@RequestBody FoodDetail foodDetail) {
        foodDetailService.updateById(foodDetail);
        return Result.ok();
    }
}
